// run: $exec < input
#include <iostream>
#include <queue>
#include <unordered_map>
#include <unordered_set>

int const maxn = 307;

int l[maxn], c[maxn];

std::unordered_map<int, int> min_cost;
int n;

int gcd(int x, int y)
{
	return !y ? x : gcd(y, x % y);
}

int main()
{
	std::ios::sync_with_stdio(false);

	std::cin >> n;
	for (int i = 0; i < n; i++) std::cin >> l[i];
	for (int i = 0; i < n; i++) std::cin >> c[i];

	std::queue<int> q;
	std::unordered_set<int> inque;
	for (int i = 0; i < n; i++) {
		if (min_cost.find(l[i]) == min_cost.end()) {
			min_cost[l[i]] = c[i];
			q.push(l[i]);
			inque.insert(l[i]);
		} else if (c[i] < min_cost[l[i]]) {
			min_cost[l[i]] = c[i];
		}
	}

	while (!q.empty()) {
		auto now = q.front(); q.pop(); inque.erase(now);
		for (int i = 0; i < n; i++) {
			int t = gcd(now, l[i]);
			if (min_cost.find(t) == min_cost.end()) {
				min_cost[t] = min_cost[now] + c[i];
				inque.insert(t);
				q.push(t);
			} else if (min_cost[now] + c[i] < min_cost[t]) {
				min_cost[t] = min_cost[now] + c[i];
				if (inque.find(t) == inque.end()) {
					inque.insert(t);
					q.push(t);
				}
			}
		}
	}

	if (min_cost.find(1) == min_cost.end())
		std::cout << "-1\n";
	else
		std::cout << min_cost[1] << '\n';
}

